// JavaScript Document
document.write('<link rel="icon" type="image/x-icon" href="/static/image/myblog.ico">');

/*请求天气预报*/
$(function() {
    var province = $('#pro').text();
    var city = $('#cit').text();
    if(province=='' || city==''){
        $('#weather-module').empty();
        return;
    }
    $.ajax({
        url:'/common/weather?province=' + encodeURI(encodeURI(province)) + '&city=' + encodeURI(encodeURI(city)),
        method:'GET',
        dataType:'json',
        contentType:'application/x-www-form-urlencoded',
        cache:false,
        async:true,
        success:function(data){
            var weather = data.weather;
            if(weather == null){
                layer.msg("暂无法查到该地区");
            }
            var dayNameArr = ['今天', '明天', '后天', '大后天'];
            var html = "";
            for(i=0;i<weather.length;i++){
                if(i==0){
                    $('#min_degree').text(weather[i].min_degree);
                    $('#max_degree').text(weather[i].max_degree);
                    $('#weather_img').attr("src", "/static/image/weather/" + weather[i].day_weather_code + ".png");
                    $('#weather_img').attr("title", weather[i].day_weather);
                }else if(i<=3){
                    html += "<tr><td><span title='" + weather[i].day_time + "'>" + dayNameArr[i] + "</span></td>" +
                        "<td><span>" + weather[i].min_degree+ "~" + weather[i].max_degree + "℃</span></td>" +
                        "<td><img src='/static/image/weather/" + weather[i].day_weather_code + ".png' title='" + weather[i].day_weather + "'></td></tr>";
                }else{
                    break;
                }
            }
            $('#weather-table').html(html);
        },
        error:function(err){
            layer.msg("天气预报数据获取失败");
        }
    });

    /*天气预报显示窗*/
    $('#today').click(function(){
        $('#window').slideToggle();
        setTimeout(function(){
            $('#window').slideUp();
        }, 15000);
    });
})

/*更新天气预报数据*/
function updateWeather(){
    $('#weather-table').html("");
    setTimeout(function(){
        var province = $('#pro').text();
        var city = $('#cit').text();
        $.ajax({
            url:'/common/weather?province=' + encodeURI(encodeURI(province)) + '&city=' + encodeURI(encodeURI(city)),
            method:'GET',
            dataType:'json',
            contentType:'application/x-www-form-urlencoded',
            cache:false,
            async:true,
            success:function(data){
                var weather = data.weather;
                if(weather == null){
                    alert("暂无法查到该地区");
                }
                var dayNameArr = ['今天', '明天', '后天', '大后天'];
                var html = "";
                for(i=0;i<weather.length;i++){
                    if(i==0){
                        $('#min_degree').text(weather[i].min_degree);
                        $('#max_degree').text(weather[i].max_degree);
                        $('#weather_img').attr("src", "/static/image/weather/" + weather[i].day_weather_code + ".png");
                        $('#weather_img').attr("title", weather[i].day_weather);
                    }else if(i<=3){
                        html += "<tr><td><span title='" + weather[i].day_time + "'>" + dayNameArr[i] + "</span></td>" +
                            "<td><span>" + weather[i].min_degree+ "~" + weather[i].max_degree + "℃</span></td>" +
                            "<td><img src='/static/image/weather/" + weather[i].day_weather_code + ".png' title='" + weather[i].day_weather + "'></td></tr>";
                    }else{
                        break;
                    }
                }
                $('#weather-table').html(html);
            },
            error:function(err){
                alert("天气预报数据获取失败");
            }
        });
    }, 200);

}
/*初始化页面消息导航*/
$(function() {
    commentLongPolling(); // 发起消息长轮询请求
    $.ajax({
            url: '/user/advices/comment/unreadcount',
        method: 'get',
        success: function(data) {
            if(data.code === 1001){
                var commentCount = data.data.unCount;
                if(commentCount > 0){
                    $('#all-badge').html('<span class="badge-all"></span>');
                    $('#comment-badge').html(commentCount);
                    if($('#comment-badge')) $('#comment-badge').html(commentCount);
                }
            }
        },
        error: function(err) {
            alert("接口回调失败")
        }
    })
})

/*向服务端发起一个长轮询请求，来接收评价消息的推送*/
function commentLongPolling() {
    $.ajax({
        url: '/poll/comment',
        method: 'get',
        success: function(data) {
            commentLongPolling();
            if(data === 'received a comment'){
                var all = $('#all-badge').html();
                if(all === '') $('#all-badge').html('<span class="badge-all"></span>');
                //获取到消息前端业务
                var commentCount = $('#comment-badge').html();
                (commentCount !== 0) ? commentCount : 0;
                $('#comment-badge').html(++commentCount);
            }else{
                return;
            }
        }
    })
}

